package com.fiestas.adapters;

import java.util.ArrayList;

import com.fiestas.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

public class UserListAdapter extends BaseAdapter{

		TextView textViewName;
		TextView textViewSurname;
		CheckBox check;
	
	private ArrayList<UserAdapter> data;
	private LayoutInflater inflater = null;
	private UserAdapter user;
	
	public UserListAdapter(Context context, ArrayList<UserAdapter> data) {
		this.data = data;
		inflater = LayoutInflater.from(context);
	}
	
	
	
	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if(convertView == null){
			convertView = inflater.inflate(R.layout.user_adapter_layout, null);
			textViewName = (TextView) convertView.findViewById(R.id.textViewUserAdapterName);
			textViewSurname = (TextView) convertView.findViewById(R.id.textViewUserAdapterSurname);
			check = (CheckBox) convertView.findViewById(R.id.checkBoxUserAdapter);
			check.setOnClickListener(checkListener);
		}else
			user = (UserAdapter) getItem(position);
			check.setTag(user);
			textViewName.setText(data.get(position).getName());
			textViewSurname.setText(data.get(position).getSurname());
			check.setChecked(data.get(position).getChecked());
			
		
		
		return convertView;
	}
	@Override
	public void notifyDataSetChanged() {
		super.notifyDataSetChanged();
	}
	
	
	private OnClickListener checkListener = new OnClickListener()
	{
 
		@Override
		public void onClick(View v)
		{
			UserAdapter d = (UserAdapter) v.getTag();
			d.setChecked(!d.getChecked());
		}
	};

}
